package com.courier.service.impl;
import com.courier.entity.User;
import com.courier.mapper.UserMapper;
import com.courier.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        return userMapper.updateStatus(id, status);
    }

    @Override
    public boolean save(User user) {
        if (user.getId() == null) {
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            return userMapper.insert(user);
        } else {
            user.setUpdateTime(LocalDateTime.now());
            return userMapper.update(user);
        }
    }

    @Override
    public boolean updateById(User user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.update(user);
    }

    @Override
    public boolean removeById(Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User getById(Long id) {
        return userMapper.findById(id);
    }

    @Override
    public List<User> list() {
        return userMapper.list();
    }
}